package com.zyt.controller;

import com.zyt.pojo.Emp;
import com.zyt.pojo.EmpQueryParam;
import com.zyt.pojo.PageResult;
import com.zyt.pojo.Result;
import com.zyt.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/*
 * 员工管理controller
 * */
@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpComtroller {
    @Autowired
    private EmpService empService;

    /*
    分页查询
     */
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询:{},", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("保存员工:{}", emp);
        empService.save(emp);
        return Result.success();
    }

    /*
    方式一:
     */
//    @DeleteMapping
//    public Result delete(Integer[] ids){
//        log.info("删除员工:{}", Arrays.toString(ids));
//        return Result.success();
//
//    }
    /*
    方式二:
     */
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工:{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    /*
    根据id查询员工
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id) {
        log.info("根据id查询员工:{}", id);
        Emp emp = empService.getinfo(id);
        return Result.success(emp);
    }

    /*
    修改员工
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工:{}", emp);
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list() {
        log.info("查询所有员工");
        List<Emp> list = empService.findAll();
        return Result.success(list);
    }

}
